<template>
  <div class="list-table">
    <!-- <div class="area-header">
      <span class="area-header-title">{{ $t('财务运营模板') }}</span>
    </div> -->

    <el-tabs v-model="view" @tab-click="handleClick">
      <el-tab-pane :label="$t('利润表')" name="income_sheet" />
      <el-tab-pane :label="$t('现金流量表')" name="cash_flow_sheet" />
      <el-tab-pane :label="$t('资产负债表')" name="balance_sheet" />
    </el-tabs>

    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-row>
        <el-form-item :label="$t('科目名字')" prop="subjectName">
          <el-input v-model="queryParams.subjectName" :placeholder="$t('科目名字')" clearable @keyup.enter.native="queryHandle" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addHandle">{{ $t('新增') }}</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button v-hasPermi="['system:temp:export']" type="warning" icon="el-icon-download" size="mini" @click="exportHandle">{{ $t('导出') }}</el-button>
      </el-col> -->
    </el-row>
    <el-row>

      <el-table
        v-loading="loading"
        :data="finSysSubjectTempList"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        height="500"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column :label="$t('科目名称')" prop="subjectName" show-overflow-tooltip>
          <template slot-scope="scope">
            <span class="custom-cell">{{ scope.row.subjectName }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('科目编码')" align="center" prop="subjectCode" />
        <el-table-column :label="$t('行次')" align="center" prop="rowNum" />
        <el-table-column :label="$t('排序')" align="center" prop="sort" />
        <el-table-column :label="$t('操作')" align="center" width="240">
          <template slot-scope="scope">
            <template v-if="scope.row.parentId=='0'">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="addHandle(scope.row)"
              >{{ $t('新增下级') }}</el-button>
            </template>
            <template>
              <el-button
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="updateHandle(scope.row)"
              >{{ $t('修改') }}</el-button>
              <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="deleteHandle(scope.row)"
              >{{ $t('删除') }}</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!-- <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" /> -->

    <!-- 添加或修改公司财务运营模板对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <finSysSubjectTempForm ref="finSysSubjectTempForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listFinSysSubjectTemp,
  delFinSysSubjectTemp,
  exportFinSysSubjectTemp
} from '@/api/fin/sysTemp/finSysSubjectTemp'
import finSysSubjectTempForm from './finSysSubjectTempForm.vue'
export default {
  components: {
    finSysSubjectTempForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 公司财务运营模板表格数据
      finSysSubjectTempList: [],
      // 弹出层标题
      title: '公司财务运营模板',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        subjectType: undefined,
        subjectName: undefined
      },
      view: 'income_sheet',
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      tempId: undefined
    }
  },
  created() {

  },
  methods: {
    /** 查询公司财务运营模板列表 */
    getList() {
      this.loading = true
      this.queryParams.subjectType = this.view
      this.queryParams.tempId = this.tempId
      listFinSysSubjectTemp(this.queryParams).then(response => {
        this.finSysSubjectTempList = response.data
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    init(row) {
      this.tempId = row.id
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        subjectType: undefined,
        subjectName: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle(row) {
      this.formOpen = true
      const data = {
        id: undefined,
        parentType: this.view,
        tempId: this.tempId,
        parentId: row.id || '0'
      }
      this.$nextTick(() => {
        this.$refs.finSysSubjectTempForm.init(data)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      row.parentType = this.view
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.finSysSubjectTempForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delFinSysSubjectTemp(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
        this.loading = false
      })
    },
    typeFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.typeOptions, row.type)
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return exportFinSysSubjectTemp(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(() => {})
    },
    handleClick() {
        this.getList()
    }
  }
}
</script>
